//
//  TMGGoldBootomViewController.m
//  TMGold
//
//  Created by Mac on 2017/12/12.
//  Copyright © 2017年 tangmu. All rights reserved.
//

#import "TMGPlanRecordViewController.h"
#import "TMGGoldBootomTableViewCell.h"
#import "TMGBuyGoldRecordModel.h"
@interface TMGPlanRecordViewController ()
@property(nonatomic, strong) NSMutableArray                     *dataSouce;
@property(nonatomic, strong) NSArray                               *titleArray;
@property(nonatomic, strong) STSegementButton                     *segementButton;
@property(nonatomic, strong) STNoresultView                     *noResultView;
@end

@implementation TMGPlanRecordViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    _titleArray = @[@"在投",@"已到期"];
    
    self.title = [self titleFromType];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self configSubView];
    // Do any additional setup after loading the view.
}
- (NSString*)titleFromType{
    if ([self.type isEqualToString:@"1"]) {
        return @"黄金资产";
    }
    if ([self.type isEqualToString:@"2"]) {
        return @"流动金";
    }
    if ([self.type isEqualToString:@"3"]) {
        return @"压箱金";
    }
    if ([self.type isEqualToString:@"4"]) {
        return @"投资计划";
    }
    if ([self.type isEqualToString:@"5"]) {
        return @"零钱计划";
    }
    if ([self.type isEqualToString:@"6"]) {
        return @"安心计划";
    }
    if ([self.type isEqualToString:@"7"]) {
        return @"月月生息";
    }
    return @"";
}
- (void)configSubView{
    __weak typeof(self) weakSelf =  self;
    
    self.segementButton = [[STSegementButton alloc] initWithFrame:CGRectMake(0, [TMUtils navgationBarBootom], UIScreenWidth, 44)
                                                         andTitle:_titleArray handle:^(STSegementButton *sender) {
                                                             [weakSelf.tableView.mj_header beginRefreshing];
                                                         }];
    self.segementButton.backgroundColor = [UIColor whiteColor];
    self.segementButton.lineSelectedColor = TM_orangeColor;
    self.segementButton.butTitleSelectedColor = TM_orangeColor;
    self.segementButton.butTitleColor = TM_secendTextColor;
    self.segementButton.lineColor = [UIColor whiteColor];
    self.segementButton.cureentIndex = 0;
    self.segementButton.autoMoveWithClic = YES;
    [self.view addSubview:self.segementButton];
    
    self.tableView.st_top = self.tableView.st_top + 44 +[TMUtils navgationBarBootom];
    self.tableView.st_height =  self.tableView.st_height - 44 - [TMUtils navgationBarBootom];
    //无数据view
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self sendFlowGoldRequest];
    }];
    [self.tableView.mj_header beginRefreshing];
    
    self.tableView.st_noreslutView = [[STNoresultView alloc] initWithFrame:CGRectMake(0, 100, UIScreenWidth, 300) title:@"暂无记录" buttonTitle:@"" buttonHandle:^(NSString *titleString) {
        
    }];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    
    if (self.type.integerValue == 5) {
        [self.segementButton removeFromSuperview];
        self.tableView.frame = UIScreenFrame;
    }
}
#pragma --mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataSouce.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return [TMGGoldBootomTableViewCell cellHeight];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 5;
}
- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}
- (UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [UIView new];
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString * reuseIdentifier =  @"cell";
    TMGGoldBootomTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (!cell) {
        cell = [[TMGGoldBootomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
        
    }
    TMGBuyGoldRecordModel * model = self.dataSouce[indexPath.row];
    cell.model = model;
    NSString * states;
    if (model.is_on.integerValue == 1) {
        states = @"在投";
    }else{
        states = @"到期";
    }
    if (self.type.integerValue < 4) {
        NSString * money = [NSString stringWithFormat:@"%@%@",
                            model.goods_number,@"克"];
        cell.subtitleLable.text = [NSString stringWithFormat:@"%@\n\n%@",money,states];
        cell.subtitleLable.attributedText = [cell.subtitleLable.text st_convertAttributeStringWithKeyWord:money attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:18],NSForegroundColorAttributeName:TM_orangeColor}];
        
        cell.timeLable.text = [NSString stringWithFormat:@"有效期至%@",model.end];
    }else{
        NSString * money = [NSString stringWithFormat:@"%@%@",@"￥",
                            model.goods_number];
        cell.subtitleLable.text = [NSString stringWithFormat:@"%@\n\n%@",money,states];
        cell.subtitleLable.attributedText = [cell.subtitleLable.text st_convertAttributeStringWithKeyWord:money attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:18],NSForegroundColorAttributeName:TM_orangeColor}];
    }

    

    return cell;
}
#pragma --mark UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    TMGBuyGoldRecordModel * model = self.dataSouce[indexPath.row];
    [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
}

- (void)sendFlowGoldRequest{
    [SVProgressHUD showWithStatus:@"请稍后"];
    NSMutableDictionary * dic = [NSMutableDictionary new];
    if ([STUserManger defult].userID.length) {
        [dic setObject:[STUserManger defult].userID forKey:@"uid"];
    }
    if ([STUserManger defult].token.length) {
        [dic setObject:[STUserManger defult].token forKey:@"token"];
    }
    if (self.type.length) {
        [dic setObject:self.type forKey:@"type"];
    }
   
    NSString * url = [NSString stringWithFormat:@"%@%@",service_header,@"/gold/public/api/detail/getTransactionRecord"];
    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 [SVProgressHUD dismiss];
                                                 NSArray * array = responseObject[@"data"][@"data"];
                                                 self.dataSouce = [NSMutableArray new];
                                                 for (NSDictionary * modelDic in array) {
                                                     TMGBuyGoldRecordModel * mdoel = [TMGBuyGoldRecordModel mj_objectWithKeyValues:modelDic];
                                                     
                                                     if (self.type.integerValue == 5) {
                                                         [self.dataSouce addObject:mdoel];
                                                     }else{
                                                         if (self.segementButton.cureentIndex == 0) {
                                                             //再投
                                                             if (mdoel.is_on.integerValue == 1) {
                                                                 [self.dataSouce addObject:mdoel];
                                                             }
                                                         }else{
                                                             if (mdoel.is_on.integerValue == 0) {
                                                                 [self.dataSouce addObject:mdoel];
                                                             }
                                                         }
                                                     }
       
                                                   
                                                 }
                                                 [self.tableView reloadData];
                                                 
                                                 [self.tableView.mj_header endRefreshing];
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 [SVProgressHUD dismiss];
                                                // [SVProgressHUD showErrorWithStatus:error.desc];
                                                 [self.tableView.mj_header endRefreshing];
                                             }];
}
@end
